#!/bin/bash

echo "🛡️ DeepGuard 系统功能测试 (邮件版)"
echo "===================================="

# 检测 Docker Compose 命令
detect_compose_cmd() {
    if docker compose version >/dev/null 2>&1; then
        COMPOSE_CMD="docker compose"
        echo "✅ 检测到 Docker Compose V2 (推荐版本)"
    elif command -v docker-compose >/dev/null 2>&1; then
        COMPOSE_CMD="docker-compose"
        echo "✅ 检测到 Docker Compose V1 (兼容模式)"
    else
        echo "❌ 错误: 未找到 Docker Compose"
        echo "请先安装 Docker Compose 或运行 ./start.sh"
        exit 1
    fi
}

# 检测 Docker Compose 命令
detect_compose_cmd

# 检查服务是否运行
echo ""
echo "1. 检查服务状态..."
cd deep-guard-docker
if ! $COMPOSE_CMD ps | grep -q "Up"; then
    echo "❌ 错误: 服务未运行，请先执行 ./start.sh"
    exit 1
fi
cd ..

echo "✅ 服务正在运行"

# 健康检查
echo ""
echo "2. 执行健康检查..."

echo "检查告警服务..."
if curl -s http://localhost:23847/health | grep -q "健康"; then
    echo "✅ 告警服务健康"
else
    echo "❌ 告警服务异常"
fi

echo "检查扫描服务..."
if curl -s http://localhost:19563/health | grep -q "健康"; then
    echo "✅ 扫描服务健康"
else
    echo "❌ 扫描服务异常"
fi

# 测试手动扫描
echo ""
echo "3. 测试手动扫描功能..."
scan_result=$(curl -s -X POST http://localhost:19563/scan)
if echo "$scan_result" | grep -q "success"; then
    echo "✅ 手动扫描功能正常"
    echo "扫描结果: $scan_result"
else
    echo "❌ 手动扫描功能异常"
    echo "错误信息: $scan_result"
fi

# 查询扫描历史
echo ""
echo "4. 查询扫描历史..."
history_result=$(curl -s http://localhost:19563/scan-results?limit=5)
if echo "$history_result" | grep -q "success"; then
    echo "✅ 扫描历史查询正常"
else
    echo "❌ 扫描历史查询异常"
fi

# 查询告警历史
echo ""
echo "5. 查询告警历史..."
alarm_history=$(curl -s http://localhost:23847/alarm-history?limit=5)
if echo "$alarm_history" | grep -q "success"; then
    echo "✅ 告警历史查询正常"
else
    echo "❌ 告警历史查询异常"
fi

# 测试病毒扫描功能
echo ""
echo "6. 测试病毒扫描功能..."
virus_scan_result=$(curl -s -X POST http://localhost:19563/virus-scan)
if echo "$virus_scan_result" | grep -q "success"; then
    echo "✅ 病毒扫描功能正常"
    echo "扫描结果: $virus_scan_result"
else
    echo "❌ 病毒扫描功能异常"
    echo "错误信息: $virus_scan_result"
fi

# 测试病毒库更新功能
echo ""
echo "7. 测试病毒库更新功能..."
update_result=$(curl -s -X POST http://localhost:19563/update-virus-db)
if echo "$update_result" | grep -q "success"; then
    echo "✅ 病毒库更新功能正常"
    echo "更新结果: $update_result"
else
    echo "❌ 病毒库更新功能异常"
    echo "错误信息: $update_result"
fi

# 测试发送告警
echo ""
echo "8. 测试发送告警功能..."
test_alarm_data='{
    "level": "高",
    "type": "测试告警",
    "details": "这是一条来自DeepGuard系统的测试告警邮件，用于验证SMTP功能是否正常工作。"
}'

echo "发送测试告警数据..."
alarm_result=$(curl -s -X POST http://localhost:23847/send-alarm \
    -H "Content-Type: application/json" \
    -d "$test_alarm_data")

if echo "$alarm_result" | grep -q '"success": true'; then
    echo "✅ 告警发送接口调用成功"
    echo "告警结果: $alarm_result"
else
    echo "❌ 告警发送接口调用失败"
    echo "错误信息: $alarm_result"
    echo ""
    echo "🔧 配置指南："
    echo "1. 检查 .env 文件中的SMTP配置是否完整且正确。"
    echo "   - SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_SENDER_EMAIL, DEFAULT_RECIPIENT_EMAIL"
    echo "2. 检查网络连接，确保告警服务容器可以访问您的SMTP服务器。"
    echo "3. 查看告警服务日志获取详细错误: cd deep-guard-docker && $COMPOSE_CMD logs alarm-service"
fi

# 查看服务状态
echo ""
echo "9. 查看服务详细状态..."
cd deep-guard-docker
echo "Docker Compose 服务状态:"
$COMPOSE_CMD ps
cd ..

echo ""
echo "🎉 测试完成！"
echo ""
echo "📊 服务访问地址:"
echo "  📡 告警服务:   http://localhost:23847"
echo "  🔍 扫描服务:   http://localhost:19563"
echo "  💾 Redis数据库: localhost:16842"
echo ""
echo "📋 管理命令:"
echo "  查看详细日志: cd deep-guard-docker && $COMPOSE_CMD logs -f"
echo "  停止服务: cd deep-guard-docker && $COMPOSE_CMD down"
echo "  重启服务: cd deep-guard-docker && $COMPOSE_CMD restart"
echo "  查看服务状态: cd deep-guard-docker && $COMPOSE_CMD ps"